package edu.gym4all.data.sqlite.android.init;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteCursor;
import android.database.sqlite.SQLiteCursorDriver;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQuery;
import edu.gym4all.service.domain.Training;
import edu.gym4all.service.domain.TrainingPlan;
import edu.gym4all.service.enums.TrainingTag;

public class SQLiteTrainingInit {

	private Context context;
	public static final String TB_TRAINING_PLAN = "tb_training_plan";

	private static final String TB_TRAINING = "tb_training";

	private SQLiteDatabase db;

	SQLiteDBInit sqlitedbinit;

	public SQLiteTrainingInit(Context context, SQLiteDatabase db) {
		this.context = context;
		this.db = db;
	}

	public void insertTraining(TrainingTag tag, SQLiteDatabase db) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(TrainingColumns.NAME, tag.toString());
		db.insert(TB_TRAINING, null, initialValues);
	}

	public void removeTraining(int id) {
		// TODO Auto-generated method stub

	}

	public void removeAll() {
		// TODO Auto-generated method stub

	}

	public List<Training> findAll(TrainingPlan trainingPlan) {
		Cursor c = this.getTrainingCursor();
		List<Training> trainings = new ArrayList<Training>();
		if (c.moveToFirst()) {
			int indexId = c.getColumnIndex(TrainingColumns.ID);
			int indexName = c.getColumnIndex(TrainingColumns.NAME);
			do {
				long trainingID = c.getLong(indexId);
				String trainingName = c.getString(indexName);
				Training training = new Training(
						TrainingTag.getTrainingTag(trainingName));
				training.setID(trainingID);
				trainings.add(training);
			} while (c.moveToNext());
		}
		c.close();
		return trainings;
	}

	// public List<Training> findAfindExerciseByIDll(long trainingID) {
	// this.sqlitedbinit = new SQLiteDBInit(this.context);
	// List<Training> trainings = new ArrayList<Training>();
	// TrainingCursor cursor = this.returnTrainings();
	// Training training;
	// if (cursor.moveToFirst()) {
	// do {
	// this.loadTrainingTags();
	// TrainingTag trainingTag = TrainingTag
	// .getTrainingTag(this.cursor.getString(1));
	// this.training = new Training(trainingTag);
	// this.trainings.add(this.training);
	// } while (this.cursor.moveToNext());
	//
	// }
	// trainings.add(new Training(TrainingTag.A));
	// return trainings;
	// }

	private Cursor getTrainingCursor() {
		try {
			return this.db.query(TB_TRAINING, new String[] { "*" }, null, null,
					null, null, null, null);
		} catch (SQLException e) {

		}
		return null;
	}

	private void loadTrainingTags() {

	}

	public TrainingCursor returnTrainings() {
		String sql = "SELECT * FROM " + TB_TRAINING_PLAN + ";";
		SQLiteDatabase bd = this.sqlitedbinit.getReadableDatabase();
		TrainingCursor trainingCursor = (TrainingCursor) bd
				.rawQueryWithFactory(new TrainingCursor.Factory(), sql, null,
						null);
		trainingCursor.moveToFirst();
		return trainingCursor;
	}

	public static class TrainingCursor extends SQLiteCursor {

		public TrainingCursor(SQLiteDatabase db, SQLiteCursorDriver driver,
				String editTable, SQLiteQuery query) {
			super(db, driver, editTable, query);
		}

		public static class Factory implements SQLiteDatabase.CursorFactory {

			@Override
			public Cursor newCursor(SQLiteDatabase db,
					SQLiteCursorDriver driver, String editTable,
					SQLiteQuery query) {
				return new TrainingCursor(db, driver, editTable, query);
			}

		}

		public long getCodTraining() {
			return this.getLong(this.getColumnIndexOrThrow("cod_training"));
		}

		public String getCodTrainingPlan() {
			return this.getString(this
					.getColumnIndexOrThrow("cod_training_plan"));
		}

		public String getTrainingName() {
			return this.getString(this.getColumnIndexOrThrow("training_name"));
		}

	}

	private static final class TrainingColumns {
		private static final String ID = "cod_training";
		private static final String NAME = "training_name";
	}
}